
// let domain = 'http://pre.runadmin.iydsj.com/background'; //187-服务器 
// let domain = 'http://test.runadmin.iydsj.com/background';//185-服务器
// let domain = 'http://runadmin.iydsj.com/background';//线上服务器
// let domain = '/api_test';
let domain = localStorage.getItem("domain") || '/api_185';
import Vue from 'vue';

export default {
	domain,

	getUrlParam(name) {
		let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		let url = window.location.href;

		let index = url.indexOf('?');
		let query = url.substr(index);

		let r = query.substr(1).match(reg);  //匹配目标参数
		if (r != null) return decodeURI(r[2]); return null; //返回参数值
	},
	toBase64(data) {

		let toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

		let base64Pad = '=';

		let result = '';

		let length = data.length;

		let i;

		// Convert every three bytes to 4 ascii characters.                                                 

		for (i = 0; i < (length - 2); i += 3) {

			result += toBase64Table[data.charCodeAt(i) >> 2];

			result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];

			result += toBase64Table[((data.charCodeAt(i + 1) & 0x0f) << 2) + (data.charCodeAt(i + 2) >> 6)];

			result += toBase64Table[data.charCodeAt(i + 2) & 0x3f];

		}

		// Convert the remaining 1 or 2 bytes, pad out to 4 characters.                                     

		if (length % 3) {

			i = length - (length % 3);

			result += toBase64Table[data.charCodeAt(i) >> 2];

			if ((length % 3) == 2) {

				result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];

				result += toBase64Table[(data.charCodeAt(i + 1) & 0x0f) << 2];

				result += base64Pad;

			} else {

				result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];

				result += base64Pad + base64Pad;

			}

		}

		return result;

	},
	//数组排序
	sortby(name) {
		return function (o, p) {
			let a, b;
			if (typeof o === "object" && typeof p === "object" && o && p) {
				a = o[name];
				b = p[name];
				if (a === b) {
					return 0;
				}
				if (typeof a === typeof b) {
					return a < b ? -1 : 1;
				}
				return typeof a < typeof b ? -1 : 1;
			} else {
				throw "error";
			}
		};
	},
	successMsg(msg, title = "成功") {
		Vue.prototype.$notify({
			type: "success",
			title: title,
			message: msg,
			offset: 80
		})
	},
	errorMsg(msg, title = "错误", type = "error") {
		Vue.prototype.$notify({
			type: type,
			title: title,
			message: msg,
			offset: 80
		})
	},
	//页面跳转；
	refreshTo(url, params) {
		if (params) {
			for (let k in params) {
				url += `&${k}=${params[k]}`;
			}
			url = url.replace("&", "?");
		}
		location.href = url;
	},
	// 存储localStorage
	setStore(name, content) {
		if (!name) return;
		if (typeof content !== 'string') {
			content = JSON.stringify(content);
		}
		window.localStorage.setItem(name, content);
	},
	// 获取localStorage
	getStore(name) {
		if (!name) return;
		return window.localStorage.getItem(name);
	},
	//删除localStorage
	removeStore(name) {
		if (!name) return;
		if (name instanceof Array) {
			name.forEach(item => {
				window.localStorage.removeItem(item);
			});
			return;
		}
		window.localStorage.removeItem(name);
	}
}

Date.prototype.format = function (format) {
	var date = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth() + 3) / 3),
		"S+": this.getMilliseconds()
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	for (var k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1
				? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
		}
	}
	return format;
}